#!/usr/bin/env python

import logging

import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.api import mail

class Registration(db.Model):
    firstName = db.StringProperty(required=True)
    lastName = db.StringProperty(required=True)
    childName = db.StringProperty(required=False)
    childDateOfBirth = db.StringProperty(required=False)
    email = db.StringProperty(required=True)
    puh = db.StringProperty(required=True)
    date = db.DateProperty(auto_now_add=True)
    groupName = db.StringProperty(required=True)
    groupTime = db.StringProperty(required=True)

class RegistrationHandler(webapp.RequestHandler):
    def get(self):
        # registrations = db.GqlQuery('SELECT * FROM Registration ORDER BY date DESC')
        # values = {'registrations': registrations}
        self.response.out.write(template.render("templates/registration.html", {}))
        
    def post(self):
        registration = Registration(
            firstName=self.request.get('firstName'),
            lastName=self.request.get('lastName'),
            childName=self.request.get('childName'),
            childDateOfBirth=self.request.get('childDateOfBirth'),
            email = self.request.get('email'),
            puh = self.request.get('puh'),
            groupName = self.request.get('groupName'),
            groupTime = self.request.get('timeSlot'))
        registration.put()
        
        logging.info('Receiving new registration', registration)
        message = mail.EmailMessage(sender="kettunen@gmail.com",
                                    subject="[LastenKupla Registration] automatic mail")
        message.to='kettunen@gmail.com, info@lastenkupla.fi'
        message.body='Registration detais \n' 
        message.body+='Ryhma: ' + registration.groupName + ' ' + registration.groupTime + '\n'
        message.body+='Huoltajan nimi: ' + registration.lastName + ', ' + registration.firstName  + '\n'
        if registration.childName is not None:
            message.body+='Lapsen nimi: ' + registration.childName + ' \n'
        if registration.childDateOfBirth is not None:
            message.body+='Lapsen syntyma-aika: ' + registration.childDateOfBirth + ' \n' 
        message.body+='Puh: ' + registration.puh + ', email: ' + registration.email + ' \n' 
        
        message.send();
        
        values = {'reg': registration}
        self.response.out.write(template.render('templates/registrationDetails.html', values))
        
class Group(db.Model):
    name = db.StringProperty(required=True)

class AdminHandler(webapp.RequestHandler):
    def get(self):
        groups = db.GqlQuery('SELECT * FROM Group ORDER BY name DESC')
        self.response.out.write(template.render('templates/admin.html', {}))
        
    def post(self):
        group = Group(name=self.request.get('name'))
        group.put()
        
        self.redirect('/admin')

class ErrorPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write(template.render('templates/errorpage.html', {}))
                
def main():
    application = webapp.WSGIApplication([('/', RegistrationHandler),
        ('/admin', AdminHandler),
        ('/.*', ErrorPage)],
        debug=True)
    wsgiref.handlers.CGIHandler().run(application)
    
if __name__ == '__main__':
    main()